<?php
/**
 *OrderCustomer
 *@author  Jiangzhendong
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class OrderCustomerController extends BaseController {
    // ordercustomer list display
    public function index(){
 		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');       
		$psize = I('get.psize', $this->m_psize,'intval');
        $p = I('get.p', '', 'intval');
        $key = I('get.key',false,'intval');
        $customer = I('get.customer','','trim');
        $is_open = I('get.is_open');
        $is_order = I('get.is_order');
        $is_forbidden = I('get.is_forbidden');
        $package = I('get.package','','trim');
        $subscribe = I('get.subscribe','','trim');
        // connection param
        $paramconnection = I('get.packagecode','','trim');

        if($key){
            if($key == 1){
                if(!$package && $package != -1){
                    if(!$paramconnection){
                        if(isset($_COOKIE['first_package'])){
                            $package = $_COOKIE['first_package'];      
                        }else{
                            $package = -1;
                        }                 
                    }else{
                            $package = $paramconnection;
                            setcookie("first_package",$package);  
                            setcookie("second_package",$package);     
                            setcookie("third_package",$package);                             
                    }
                }else{
                    setcookie("first_package",$package);         
                }
            }else if($key == 10){
                if(!$package && $package != -1){
                    if(!$paramconnection){
                        if(isset($_COOKIE['second_package'])){
                            $package = $_COOKIE['second_package'];      
                        }else{
                            $package = -1;
                        }                 
                    }else{
                            $package = $paramconnection;
                            setcookie("second_package",$package);                               
                    }
                }else{
                    setcookie("second_package",$package);         
                }
            }else{
                if(!$package && $package != -1){
                    if(!$paramconnection){
                        if(isset($_COOKIE['third_package'])){
                            $package = $_COOKIE['third_package'];      
                        }else{
                            $package = -1;
                        }                 
                    }else{
                            $package = $paramconnection;
                            setcookie("third_package",$package);                               
                    }
                }else{
                    setcookie("third_package",$package);         
                }
            }
        }

        if($key){
            if($key == 1){
                if(!$subscribe && $subscribe != -1){
                    if(isset($_COOKIE['first_qy_account'])){
                        $subscribe = $_COOKIE['first_qy_account'];
                    }else{
                        $subscribe = -1;
                    }
                }else{
                    setcookie("first_qy_account",null);
                    setcookie("first_qy_account",$subscribe);
                }
            }else if($key == 10){
                if(!$subscribe && $subscribe != -1){
                    if(isset($_COOKIE['second_qy_account'])){
                        $subscribe = $_COOKIE['second_qy_account'];
                    }else{
                        $subscribe = -1;
                    }
                }else{
                    setcookie("second_qy_account",null);
                    setcookie("second_qy_account",$subscribe);
                }
            }else{
                if(!$subscribe && $subscribe != -1){
                    if(isset($_COOKIE['third_qy_account'])){
                        $subscribe = $_COOKIE['third_qy_account'];
                    }else{
                        $subscribe = -1;
                    }
                }else{
                    setcookie("third_qy_account",null);
                    setcookie("third_qy_account",$subscribe);
                }                
            }
        }

        $sql = "1=1";
        if($key){
            $sql.=" and qy_subscriber_type = ".$key;
        }

        if($customer){
            $sql.= " and ( qy_subscriber_userid  like '%" . $customer . "%' or qy_subscriber_name like '%" . $customer . "%')";
        }

        if($is_open || $is_open == 0 && $is_open != null){
            if( $is_open != -1 ){
                $sql.=" and qy_subscriber_sync = ".$is_open;                   
            }
        }
        if($is_order || $is_order == 0  && $is_order != null){
            if( $is_order != -1 ){
                $sql.=" and qy_subscriber_guanzhu = ".$is_order;               
            }                

        }
        if($is_forbidden || $is_forbidden == 0  && $is_forbidden != null){
            if( $is_forbidden != -1 ){
                $sql.=" and qy_subscriber_ban = ".$is_forbidden;               
            }

        }
        if($package && $package != null){
            if($package != -1){
                $callfunction  = M('product_package')->field('qy_deparment_id')->where(array('package_code'=>$package,'is_deleted'=>array('neq',1)))->find();
                $sql.=" and FIND_IN_SET('".$callfunction['qy_deparment_id']."',qy_subscriber_department)>0";
            }
        }
        if($subscribe && $subscribe != null){
            if($subscribe != -1){
                $sql.=" and qy_code = ".'"'.$subscribe.'"';
            }
        }

        if( !$psize )
            $psize = $this->m_psize;
        else if( $psize > 20 )
            $psize = $this->m_psize;

        $cnt =  M('qy_subscriber')->where($sql)->count();
        $Page = new \Common\ORG\Page( $cnt, $psize );
      
        $callfun =  M('qy_subscriber')
                                         ->where($sql)
                                         ->limit($Page->firstRow.','.$Page->listRows )
                                        //  ->order('create_time desc')
                                         ->select();
        /**2017-07-04 wjw 添加客户浮窗开始**/
        foreach ($callfun as &$value) {
            if($value['qy_subscriber_type']==1){
                $where['customer_code'] = $value['qy_subscriber_userid'];
                $customer = M("customer")->field("customer_id")->where($where)->find();
                $value['customer_id'] = $customer['customer_id'];
            }
        }
        /**添加客户浮窗结束**/
        if($callfun){
			$res['data'] = $callfun;
			$res['rcode'] = 1;
			$res['pall'] = $Page->getTotalPages();
			$res['p'] = $p;
			$res['psize'] = $psize;
			$res['sql'] = M('qy_subscriber')->getlastsql();
            $res['subscribe'] = $subscribe;
            $res['package'] = $package;
        }else{
            $res['subscribe'] = $subscribe;
            $res['package'] = $package;
            $res['sql'] = M('qy_subscriber')->getlastsql();
        }
        e_json($res);
    }

    // get user form sector
    public function GetUser(){
        $res = array('rcode'=>0,'data'=>NULL, 'msg'=> '');
        $sector_id = I('get.sector_id',false,'intval');
        $callsector = M('user') ->field('user_id,login_name,nickname')
                                ->where(array('sector_id'=>$sector_id))
                                ->select();
        if($callsector){
            $res['rcode'] = 1;
            $res['data'] = $callsector;
        }
        e_json($res);
    }

    // get subcompany 
    public function getsubcompany(){
        $res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
        $wheres = array();
		$wheres['is_deleted'] = array('neq', 1);
	   $MSector = D('Admin/Sector');
	   $secs = $MSector->field("sector_id,info,sector_name,parent_company,parent_sector,manager_id")->where( $wheres )->select();
       $_secs = array();
    	foreach ($secs as $sc ){
            $_secs[$sc['sector_id']] = $sc;
        }
        $subcompany = list_to_tree($secs,'sector_id','parent_sector','_child', 0 );
        if($subcompany){
            $res['rcode'] = 1;
            $res['data'] = $subcompany;
        }
        e_json($res);
    }

    // get GetCompanyAccount 
    public function GetCompanyAccount(){
        $res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
        $callfun = M('qy')->field('qy_code,qy_name')->where(array('is_deleted'=>array('neq',1)))->select();
        foreach($callfun as $key => $val){
            if($key == 0){
                $callfun[$key]['qy_code'] = -1;
                $callfun[$key]['qy_name'] = '全部';
            }
            $callfun[$key+1]['qy_code'] = $val['qy_code'];
            $callfun[$key+1]['qy_name'] = $val['qy_name'];            
        }
        if($callfun){
            $res['rcode'] = 1;
            $res['data'] = $callfun;
            array_shift($callfun);
            $res['redata'] = $callfun;
        }
        e_json($res);
    }

    // get package
    public function GetPackage(){
        $res = array('rcode'=>0,'data'=>NULL, 'search'=>NULL,'msg'=> 'OK');
        if( IS_GET){
            $callfun = M('product_package')
                                            ->field('package_code,name,qy_deparment_id')
                                            ->where(array('is_deleted'=>array('neq',1)))
                                            ->order('qy_deparment_id asc')
                                            ->select();  
            foreach($callfun as $key => $val){
                if($key == 0){
                    $callfun[$key]['name'] = '全部';
                    $callfun[$key]['package_code'] = -1;
                    $callfun[$key]['qy_deparment_id'] = -1;
                }
                $callfun[$key+1]['name'] = $val['name'];
                $callfun[$key+1]['package_code'] = $val['package_code'];
                $callfun[$key+1]['qy_deparment_id'] = $val['qy_deparment_id'];  
            }
        }else{
            $arr = array();
            $package = I('post.package','','trim');
            $callfunres = M('qy')  ->alias('a')
                                ->distinct(true)
                                ->field('d.package_code,d.name,d.qy_deparment_id')
                                ->join('LEFT JOIN qy_app as b ON b.qy_code = a.qy_code')
                                ->join('LEFT JOIN qy_app_package as c ON c.qy_app_id = b.qy_app_id')
                                ->join('LEFT JOIN product_package as d ON d.package_code = c.package_code')
                                ->where(array('a.qy_code'=>$package,'d.is_deleted'=>array('neq',1)))
                                ->select();
        }
       
        if($callfun || $callfunres){
            $res['rcode'] = 1;
            $res['data'] = $callfun;
            $res['res'] = $callfunres;
        }
        e_json($res);        
    }

    // forbide subscribe
    public function SubscriberForbidden(){
       $res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
       $key = I('post.key',false,'intval');
       $val = I('post.val',0,'intval');
       $callfun = M('qy_subscriber')->where(array('qy_subscriber_id'=>$key))->save(array('qy_subscriber_enable'=>$val));
       if($callfun){
            $res['rcode'] = 1;
            $res['data'] = M('qy_subscriber')->getLastSql();
       }
       e_json($res);
    }

    // delete subscribe
    public function SubscriberDelete(){
        $res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
        $key = I('post.key',false,'intval');
        if($key){
            $callfun = M('qy_subscriber')->where(array('qy_subscriber_id'=>$key))->delete();
        }
        if($callfun){
            $res['rcode'] = 1;
        }
        e_json($res);
    }

    // modify subscriber
    public function ModifyUser(){
        $res = array('rcode'=>0,'data'=>NULL,'code'=>array(),'name'=>array(),'deparment_id'=>array(),'qy_code'=>array(),'msg'=> 'OK');
        if(IS_GET){
            // $arr = array();
            $key = I('get.id',false,'intval');
            if($key){
                $callfun = M('qy_subscriber') ->where(array('qy_subscriber_id'=>$key))->find();
                if($callfun){
                    $arr = explode(',',$callfun['qy_subscriber_department']);
                    if(!empty($arr)){
                        $callfunpro = M('product_package')->field('package_code,name,qy_deparment_id')->where(array('qy_deparment_id'=>array('IN',$arr)))->select();
                        if($callfunpro){
                            // $res['pro']=$callfunpro;
                            foreach($callfunpro as $key => $val){
                                $res['code'][] = $val['package_code'];
                                $res['name'][] = $val['name'];
                                $res['deparment_id'][] = $val['qy_deparment_id'];
                            }
                        }
                    }

                    $qy_code = $callfun['qy_code']; 
                    if($qy_code){
                        $callfunres = M('qy')  ->alias('a')
                                            ->field('d.package_code,d.name,d.qy_deparment_id')
                                            ->join('LEFT JOIN qy_app as b ON b.qy_code = a.qy_code')
                                            ->join('LEFT JOIN qy_app_package as c ON c.qy_app_id = b.qy_app_id')
                                            ->join('LEFT JOIN product_package as d ON d.package_code = c.package_code')
                                            ->where(array('a.qy_code'=>$qy_code,'d.is_deleted'=>array('neq',1)))
                                            ->select();   
                        if($callfunres){
                            $res['qy_code'] = $callfunres;
                        }
                    }
                 
                    $res['rcode'] = 1;
                    $res['data'] = $callfun;
                    // $res['arr'] = $arr;
                }
            }
        }else{
            $wheres = array();
            $_POST['editparam'] ? $editsecondparams = $_POST['editparam'] : $editsecondparams = '';
            if($editsecondparams){
                if(!empty($editsecondparams['subscribe'])){
                    $subscribe = implode(',',$editsecondparams['subscribe']);
                    $wheres['qy_subscriber_department'] = $subscribe;
                }else{
                    $wheres['qy_subscriber_department'] = '';
                }
                if($editsecondparams['qy_subscriber_mobile']){
                    $wheres['qy_subscriber_mobile'] = $editsecondparams['qy_subscriber_mobile'];
                }else{
                    $wheres['qy_subscriber_mobile'] = '';
                }
                if($editsecondparams['qy_subscriber_weixinid']){
                    $wheres['qy_subscriber_weixinid'] = $editsecondparams['qy_subscriber_weixinid'];
                }else{
                    $wheres['qy_subscriber_weixinid'] = '';
                }
                if($editsecondparams['start_time'] && $editsecondparams['end_time']){
                    $tm_s = strtotime( $editsecondparams['start_time'] );
                    $tm_e = strtotime( $editsecondparams['end_time'] );
                    if( $tm_e < $tm_s ){
                        // break;
                        e_json($res);
                    }
                    else{
                        $stime = date('Y-m-d ', $tm_s );
                        $etime =  date('Y-m-d ', $tm_e );
                        $wheres['start_time'] = $stime;
                        $wheres['end_time'] = $etime;
                    }  
                }else if(!$editsecondparams['start_time'] && !$editsecondparams['end_time']){
                    $wheres['start_time'] = null;
                    $wheres['end_time'] = null;
                }

                $callfunedit = M('qy_subscriber')->where(array('qy_subscriber_id'=>$editsecondparams['qy_subscriber_id']))->save($wheres);
                // if($callfunedit){
                    $res['rcode'] = 1;
                // }
            }
            // e_json($_POST);
        }
        e_json($res);
    }

    // add user
    public function AddUser(){
        $res = array('rcode'=>0,'data'=>'','msg'=>'');
        $wheres = $arr = $package = $qyarr = $qa = $qain = $qynickname = array();
        $_POST['addsecondparam'] ? $addsecondparam = $_POST['addsecondparam'] : $addsecondparam = '';
        $wheres['qy_subscriber_type'] = 10;
        $wheres['qy_subscriber_enable'] = 1;
        $wheres['qy_subscriber_status'] = 10;
        if($addsecondparam){
            if($addsecondparam['stime'] && $addsecondparam['etime'] ){
                $tm_s = strtotime( $addsecondparam['stime'] );
                $tm_e = strtotime( $addsecondparam['etime'] );
                if( $tm_e < $tm_s ){
                    // break;
                    e_json($res);
                }
                else{
                    $stime = date('Y-m-d', $tm_s );
                    $etime =  date('Y-m-d ', $tm_e );
                    $wheres['start_time'] = $stime;
                    $wheres['end_time'] = $etime;
                }
            }
            if($addsecondparam['addpck']){
                foreach($addsecondparam['addpck'] as $k => $v){
                    $package[] = $v;
                }
                if($package){
                    $add_subscriber_department = implode(',',$package);
                    $wheres['qy_subscriber_department'] = $add_subscriber_department;                    
                }
            }
            if($addsecondparam['companyaccount']){
                $wheres['qy_code'] = $addsecondparam['companyaccount'];
            }
            if($addsecondparam['adduser'] && $addsecondparam['companyaccount']){
                //  $callfunqy = M('qy_subscriber')->field('qy_subscriber_userid')->where(array('qy_subscriber_userid'=>array('IN',$addsecondparam['adduser']),'qy_code'=>$addsecondparam['companyaccount'],'qy_subscriber_type'=>array('eq',10)))->select();
                 $callfunqy = M('qy_subscriber')->field('qy_subscriber_userid')->where(array('qy_subscriber_userid'=>array('IN',$addsecondparam['adduser']),'qy_code'=>$addsecondparam['companyaccount']))->select();
                 if(!empty($callfunqy)){
                    foreach($callfunqy as $kq => $vq){
                        $qa[] = $vq['qy_subscriber_userid'];
                    }
                 }
                 if(!empty($qa)){
                    foreach($addsecondparam['adduser'] as $k => $v){
                        if(!in_array($v,$qa)){
                            $qyarr[] = $v;
                        }else{
                            $qain[] = $v;
                        }
                    }
                    if(!empty($qyarr)){
                        $callfun = M('user')->field('login_name,nickname,phone,email,weixin,sex')->where(array('login_name'=>array('in',$qyarr)))->select();
                        if(!empty($callfun)){
                            foreach($callfun as $key => $val){
                                $wheres['qy_subscriber_userid'] = $val['login_name'];
                                $wheres['qy_subscriber_name'] = $val['nickname'];
                                $wheres['qy_subscriber_mobile'] = $val['phone'];
                                $wheres['qy_subscriber_email'] = $val['email'];
                                $wheres['qy_subscriber_weixinid'] = $val['weixin'];
                                $wheres['qy_subscriber_gender'] = $val['sex'];
                                $callfunadd = M('qy_subscriber')->add($wheres);
                                if($callfunadd){
                                    $arr[] = 1;
                                }else{
                                    $arr[] = 2;
                                }
                            }
                        }
                    }else{
                        $arr[] = 1;
                    }
                    // add repeating user alert
                    if(!empty($qain)){
                        $callfunqyuser = M('user')->field('nickname')->where(array('login_name'=>array('IN',$qain)))->select();
                        if(!empty($callfunqyuser)){
                            foreach($callfunqyuser as $key=>$val){
                				if($key == 0){
                					$callfunnickname = $val['nickname'];
                				}else{
                					$callfunnickname = $callfunnickname.'、'.$val['nickname'];
                				}
                                if($callfunnickname){
                                    $res['nickname'] = $callfunnickname;
                                }else{
                                    $res['nickname'] = '';
                                }
                            }
                        }
                    }
                 }else{
                    $callfun = M('user')->field('login_name,nickname,phone,email,weixin,sex')->where(array('login_name'=>array('in',$addsecondparam['adduser'])))->select();
                    if(!empty($callfun)){
                        foreach($callfun as $key => $val){
                            $wheres['qy_subscriber_userid'] = $val['login_name'];
                            $wheres['qy_subscriber_name'] = $val['nickname'];
                            $wheres['qy_subscriber_mobile'] = $val['phone'];
                            $wheres['qy_subscriber_email'] = $val['email'];
                            $wheres['qy_subscriber_weixinid'] = $val['weixin'];
                            $wheres['qy_subscriber_gender'] = $val['sex'];
                            $callfunadd = M('qy_subscriber')->add($wheres);
                            if($callfunadd){
                                $arr[] = 1;
                            }else{
                                $arr[] = 2;
                            }
                        }
                    }                     
                 }
            }
        }

        if(!empty($arr)){
            if(!in_array(2,$arr)){
                $res['rcode'] = 1;
                $res['callfun'] = $callfun;
            }
        }
        e_json($res);
    }

    // add stranger
    public function AddStranger(){
        $res = array('rcode'=>0,'data'=>'','msg'=>'');
        $wheres = $arr = $package = array();
        $_POST['thirdparam'] ? $thirdparam = $_POST['thirdparam'] : $thirdparam = '';
        $wheres['qy_subscriber_type'] = 20;
        $wheres['qy_subscriber_enable'] = 1;
        $wheres['qy_subscriber_status'] = 10;
        $maxQysubscriberuserid = M('qy_subscriber')->where(array('qy_subscriber_type'=>20))->max('qy_subscriber_userid');
        $wheres['qy_subscriber_userid'] = $maxQysubscriberuserid + 1;
        $wheres['qy_subscriber_position'] = '合作伙伴';
        if($thirdparam){
            if($thirdparam['stime'] && $thirdparam['etime'] ){
                $tm_s = strtotime( $thirdparam['stime'] );
                $tm_e = strtotime( $thirdparam['etime'] );
                if( $tm_e < $tm_s ){
                    // break;
                    e_json($res);
                }
                else{
                    $stime = date('Y-m-d', $tm_s );
                    $etime =  date('Y-m-d', $tm_e );
                    $wheres['start_time'] = $stime;
                    $wheres['end_time'] = $etime;
                }
            }
            if($thirdparam['addpck']){
                foreach($thirdparam['addpck'] as $k => $v){
                    $package[] = $v;
                }
                if($package){
                    $add_subscriber_department = implode(',',$package);
                    $wheres['qy_subscriber_department'] = $add_subscriber_department;                    
                }
            }
            if($thirdparam['companyaccount']){
                $wheres['qy_code'] = $thirdparam['companyaccount'];
            }
            $wheres['qy_subscriber_name'] = $thirdparam['customername'];
            $wheres['qy_subscriber_mobile'] = $thirdparam['phone'];
            $wheres['qy_subscriber_weixinid'] = $thirdparam['wechat'];
            $wheres['qy_subscriber_gender'] = 0;
            $callfunadd = M('qy_subscriber')->add($wheres);
        }
        if($callfunadd){
            $res['rcode'] = 1;
        }
        e_json($res);
    }


    // modify stranger
    public function ModifyStranger(){
        $res = array('rcode'=>0,'data'=>NULL,'code'=>array(),'name'=>array(),'deparment_id'=>array(),'msg'=> 'OK');
        if(IS_GET){
            // $arr = array();
            $key = I('get.id',false,'intval');
            if($key){
                $callfun = M('qy_subscriber') ->where(array('qy_subscriber_id'=>$key))->find();
                if($callfun){
                    // foreach($callfun as $key => $val){
                        $arr = explode(',',$callfun['qy_subscriber_department']);
                        if(!empty($arr)){
                            $callfunpro = M('product_package')->field('package_code,name,qy_deparment_id')->where(array('qy_deparment_id'=>array('IN',$arr)))->select();
                            if($callfunpro){
                                // $res['pro']=$callfunpro;
                                foreach($callfunpro as $key => $val){
                                    $res['code'][] = $val['package_code'];
                                    $res['name'][] = $val['name'];
                                    $res['deparment_id'][] = $val['qy_deparment_id'];
                                }
                            }
                        }
                    // }
                    $res['rcode'] = 1;
                    $res['data'] = $callfun;
                    // $res['arr'] = $arr;
                }
            }
        }else{
            $wheres = array();
            $_POST['editparam'] ? $editthirdparams = $_POST['editparam'] : $editthirdparams = '';
            if($editthirdparams){
                if(!empty($editthirdparams['subscribe'])){
                    $subscribe = implode(',',$editthirdparams['subscribe']);
                    $wheres['qy_subscriber_department'] = $subscribe;
                }else{
                    $wheres['qy_subscriber_department'] = '';
                }
                if($editthirdparams['qy_subscriber_mobile']){
                    $wheres['qy_subscriber_mobile'] = $editthirdparams['qy_subscriber_mobile'];
                }else{
                    $wheres['qy_subscriber_mobile'] = '';
                }
                if($editthirdparams['qy_subscriber_weixinid']){
                    $wheres['qy_subscriber_weixinid'] = $editthirdparams['qy_subscriber_weixinid'];
                }else{
                    $wheres['qy_subscriber_weixinid'] = '';
                }
                if($editthirdparams['qy_subscriber_name']){
                    $wheres['qy_subscriber_name'] = $editthirdparams['qy_subscriber_name'];
                }else{
                      $wheres['qy_subscriber_name'] = '';
                }
                if($editthirdparams['start_time'] && $editthirdparams['end_time']){
                    $tm_s = strtotime( $editthirdparams['start_time'] );
                    $tm_e = strtotime( $editthirdparams['end_time'] );
                    if( $tm_e < $tm_s ){
                        // break;
                        e_json($res);
                    }
                    else{
                        $stime = date('Y-m-d', $tm_s );
                        $etime =  date('Y-m-d', $tm_e );
                        $wheres['start_time'] = $stime;
                        $wheres['end_time'] = $etime;
                    }  
                }else{
                    $wheres['start_time'] = null;
                    $wheres['end_time'] = null;                  
                }

                $callfunedit = M('qy_subscriber')->where(array('qy_subscriber_id'=>$editthirdparams['qy_subscriber_id']))->save($wheres);
                // if($callfunedit){
                    $res['rcode'] = 1;
                // }
            }
            // e_json($_POST);
        }
        e_json($res);     
    }

    // modify customer
    public function ModifyCustomer(){
        $res = array('rcode'=>0,'data'=>NULL,'code'=>array(),'name'=>array(),'deparment_id'=>array(),'msg'=> 'OK');
        if(IS_GET){
            // $arr = array();
            $key = I('get.id',false,'intval');
            if($key){
                $callfun = M('qy_subscriber') ->where(array('qy_subscriber_id'=>$key))->find();
                if($callfun){
                    $arr = explode(',',$callfun['qy_subscriber_department']);
                    if(!empty($arr)){
                        $callfunpro = M('product_package')  ->alias('a')
                                                            ->field('a.name as packagename,a.product_id,b.name')
                                                            ->join('LEFT JOIN product as b ON b.product_id = a.product_id')
                                                            ->where(array('a.qy_deparment_id'=>array('IN',$arr)))
                                                            ->select();
                    }
                    $res['rcode'] = 1;
                    $res['data'] = $callfun;
                    $res['pro'] = $callfunpro;
                }
            }
        }else{
            $wheres = array();
            $_POST['editparam'] ? $editfirstparams = $_POST['editparam'] : $editfirstparams = '';
            if($editfirstparams){
                if($editfirstparams['qy_subscriber_mobile']){
                    $wheres['qy_subscriber_mobile'] = $editfirstparams['qy_subscriber_mobile'];
                }else{
                    $wheres['qy_subscriber_mobile'] = '';
                }
                if($editfirstparams['qy_subscriber_weixinid']){
                    $wheres['qy_subscriber_weixinid'] = $editfirstparams['qy_subscriber_weixinid'];
                }else{
                    $wheres['qy_subscriber_weixinid'] = '';
                }
                $callfunedit = M('qy_subscriber')->where(array('qy_subscriber_id'=>$editfirstparams['qy_subscriber_id']))->save($wheres);
                // if($callfunedit){
                    $res['rcode'] = 1;
                // }
            }
        }
        e_json($res);       
    }

    // avoide wechat msg duplicate
    public function wechat_duplicate(){
        $val = I('post.data','','trim');
        $companyaccount = I('post.companyaccount','','trim');
        if($val){
            if($companyaccount){
                $callfun = M('qy_subscriber')->where(array('qy_subscriber_weixinid'=>$val,'qy_code'=>$companyaccount))->count();
                if($callfun){
                    $res = false;
                }else{
                    $res = true;
                }
            }else{
                $res = true;
            }
        }else{
            $res = true;
        }
        e_json($res);
    }

    // avoide phone msg duplicate
    public function phone_duplicate(){
        $val = I('post.data','','trim');
        $companyaccount = I('post.companyaccount','','trim');
        if($val){
            if($companyaccount){
                $callfun = M('qy_subscriber')->where(array('qy_subscriber_mobile'=>$val,'qy_code'=>$companyaccount))->count();
                if($callfun){
                    $res = false;
                }else{
                    $res = true;
                }  
            }else{
                $res = true;
            }
        }else{
            $res = true;
        }
        e_json($res);
    }

    // avoid to edit wechat num duplicate
    public function edit_wechat_duplicate(){
        $val = I('post.data','','trim');
        $id = I('post.id',false,'intval');
        $qy_code = I('post.qy_code','','trim');
        if($val){
            if($id){
                $callfun = M('qy_subscriber')->field('qy_subscriber_weixinid')->where(array('qy_subscriber_id'=>$id))->find();
                if($callfun){
                    if($callfun['qy_subscriber_weixinid'] == $val){
                        $res = true;
                    }else{
                        if($qy_code){
                            $callfunwechat = M('qy_subscriber')->where(array('qy_subscriber_weixinid'=>$val,'qy_code'=>$qy_code))->count();
                            if($callfunwechat){
                                $res = false;
                            }else{
                                $res = true;
                            }
                        }

                    }
                }               
            }
        }else{
            $res = true;
        }
        e_json($res);
    }

    // avoid to edit phone num duplicate
    public function edit_phone_duplicate(){
        $val = I('post.data','','trim');
        $id = I('post.id',false,'intval');
        $qy_code = I('post.qy_code','','trim');
        if($val){
            if($id){
                $callfun = M('qy_subscriber')->field('qy_subscriber_mobile')->where(array('qy_subscriber_id'=>$id))->find();
                if($callfun){
                    if($callfun['qy_subscriber_mobile'] == $val){
                        $res = true;
                    }else{
                        if($qy_code){
                            $callfunphone = M('qy_subscriber')->where(array('qy_subscriber_mobile'=>$val,'qy_code'=>$qy_code))->count();
                            if($callfunphone){
                                $res = false;
                            }else{
                                $res = true;
                            }
                        }
                    }
                }               
            }
        }else{
            $res = true;
        }
        e_json($res);
    }
}
?>